package gcu.module.property_apply.service;

import com.google.common.collect.Maps;
import gcu.common.context.SecurityContextHolder;
import gcu.module.build.dao.BuildDao;
import gcu.module.build.daomain.Build;
import gcu.module.owner.daomain.Owner;
import gcu.module.owner.service.OwnerService;
import gcu.module.property_apply.dao.ApplyDao;
import gcu.module.property_apply.daomain.PropertyApply;
import gcu.module.rbac.daomain.User;
import gcu.module.rbac.service.UserService;
import gcu.util.Pager;
import gcu.util.SFormt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by haol on 2016/9/24.
 */
@Service
public class ApplyService {
      @Autowired
      private ApplyDao applyDao;
      @Autowired
      private BuildDao buildDao;
      @Autowired
      private OwnerService ownerService;

      public List<PropertyApply> queryApplyList(PropertyApply apply, Pager pager) {
            return applyDao.queryApplyList(apply, pager);
      }

      public void add(PropertyApply apply) {

            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(SFormt.NOWDATE_yMd());
            Owner owner = ownerService.queryOwnerByName(apply.getApplyName());
            String ownerId = String.format("%5d",owner.getId());
            stringBuffer.append(ownerId);//业主id
            stringBuffer.append("USERID");//操作者id
            apply.setIdentifier(stringBuffer.toString());

            apply.setApplyTime(SFormt.NOWDATE_y_M_dhms());
            apply.setHandleResult(0);
            apply.setIsRead(0);
            //该业主所在楼层负责人
            Build build = buildDao.queryBuildByOwnerName(apply.getApplyName());
            apply.setResponsible(build.getResponsible());

            applyDao.addApply(apply);
      }

      public void update(PropertyApply apply) {
            apply.setHandleTime(SFormt.NOWDATE_y_M_dhms());
            User user = SecurityContextHolder.getUser();
            apply.setHandleName(user.getRealName());
            applyDao.updateApply(apply);
      }
      public void setApplyIsRead(int id) {

            applyDao.setApplyIsRead(id);
      }

      public void deleteApply(int id) {
            applyDao.deleteApply(id);
      }


      public PropertyApply queryApplyById(Integer id) {
            return applyDao.queryApplyById(id);
      }

      @Transactional(readOnly = true)
      public Map<String, Object> get(Integer id) {
            Map<String, Object> map = Maps.newHashMap();
            if (id == null) {
                  map.put("apply", new PropertyApply());
            } else {
                  PropertyApply apply = applyDao.queryApplyById(id);
                  map.put("apply", apply);
            }
            return map;
      }

      public List<PropertyApply> getNotHandle(){
            return applyDao.getNotHandle();
      }
}
